﻿package com.railway.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.org.rapid_framework.util.holder.ApplicationContextHolder;

import com.railway.common.constants.SysConfigBean;
import com.railway.common.constants.SysConstant;
import com.railway.msg.constants.IConstant;
import com.railway.news.model.Menu;
import com.railway.news.service.IMenuManager;
import com.railway.system.model.SysUser;
import com.railway.system.service.ISysUserManager;

 
public class StaticMap {

	static public Map SYS_USER = new ConcurrentHashMap();
	static public Map SYS_DICT = null;
	static public Map SYS_MENU =null;
	
	static public Map MSG_STATUS =null;
	/**
	 * 系统初始化，基础信息放入缓存中
	 */
	public static void initInfor(ApplicationContext ctx) {
		MSG_STATUS = new HashMap();
		MSG_STATUS.put(IConstant.MSG_STATUS_DELETED, "已删除");
		MSG_STATUS.put(IConstant.MSG_STATUS_READED, "已读");
		MSG_STATUS.put(IConstant.MSG_STATUS_UNREAD, "未读");
		/*
		 * getSysNetInf(); // 系统类别网元类型 getRoomInf(); // 机房信息 getVendorInf(); //
		 * 厂商信息 getAddonsInf(); // 附加表信息 getAddonsRank();
		 */
		
		SysConfigBean cfgbean = (SysConfigBean)ctx.getBean("cfgBean");    
		System.out.println(cfgbean.getMsg_upload_path());   
		SysConstant.setMsgUplaodPath(cfgbean.getMsg_upload_path());
		
		System.out.println(cfgbean.getVideo_upload_path());   
		SysConstant.setVideoUplaodPath(cfgbean.getVideo_upload_path());
		getUser(ctx);
		//getDept(ctx);
		getMenu(ctx);
	}

	public static void getUser(ApplicationContext ctx) {
		 
		ISysUserManager mgr = (ISysUserManager) ctx.getBean("sysUserManager");
		List<SysUser> list = mgr.findAll();

		Map hashtable = new ConcurrentHashMap();
		for (int j = 0; j < list.size(); j++) {
			SysUser sysUser = (SysUser) list.get(j);
			hashtable.put(sysUser.getId(), sysUser.getUserName());
		}
		SYS_USER = hashtable;
	}
	public static void getMenu(ApplicationContext ctx){
		IMenuManager mgr=(IMenuManager)ctx.getBean("menuManager");
		List<Menu> list=mgr.findAll();
		Map map = new ConcurrentHashMap();
		//页顶标题
		List<Menu> topTitle=new ArrayList();
		//菜单
		List<Menu> menus=new ArrayList();
		//左侧标题
		List<Menu> leftTitle=new ArrayList();
		//友情链接
		List<Menu> friendLink=new ArrayList();
		
		if(list!=null){
			for(int i=0;i<list.size();i++){
				Menu menu=(Menu)list.get(i);
				if(menu.getLocations()==null){
				}else if(menu.getLocations().equals("topTitle")){
					topTitle.add(menu);
				}else if(menu.getLocations().equals("menus")){
					menus.add(menu);
				}else if(menu.getLocations().equals("leftTitle")){
					leftTitle.add(menu);
				}else if(menu.getLocations().equals("friendLink")){
					friendLink.add(menu);
				}
			}
		}
		
		//递归整理菜单
		menus=getAllChildrenByid(menus);
		
		map.put("topTitle", topTitle);
		map.put("menus", menus);
		map.put("leftTitle", leftTitle);
		map.put("friendLink", friendLink);
		
		SYS_MENU=map;
		
	}
	//递归处理菜单方法
	public static List<Menu> getAllChildrenByid(List<Menu> datas) {
		if (datas == null)
			return null;
		List<Menu> menus = new ArrayList<Menu>();
		for (int i = 0; i < datas.size(); i++) {
			Menu temp = datas.get(i);
			if (temp.getParenId() == null || temp.getParenId().equals("-1")) {
				menus.add(temp);
			}
			else {
				for (int j = 0; j < datas.size(); j++) {
					Menu ptemp = datas.get(j);
					if (ptemp.getId().equals(temp.getParenId())) {
						List<Menu> pmenus = ptemp.getChildrenMenu();
						if (pmenus == null)
							pmenus = new ArrayList<Menu>();
						if (!pmenus.contains(temp))
							pmenus.add(temp);
						ptemp.setChildrenMenu(pmenus);
					}
				}
			}
		}
		return menus;
	}
	/*public static void getDept() {
		ISysDeptManager mgr = (ISysDeptManager) ApplicationContextHolder
				.getBean("ItawSystemDeptManager");
		List list = mgr.geDeptdelInfos("0");

		Hashtable hashtable = new Hashtable();
		for (int j = 0; j < list.size(); j++) {
			SysDept tawSystemDept = (SysDept) list.get(j);
			hashtable.put(tawSystemDept.getId(), tawSystemDept.getDeptName());
		}
		SYS_DEPT = hashtable;
	}*/

	public static void getDict(ApplicationContext ctx) {
		/*ISysDictManager mgr = (ISysDictManager) ctx.
				getBean("sysDictManager");
		SysDict tawSystemDict = new SysDict();
		List<SysDict> list = mgr.findAll();
		Hashtable hashtable = new Hashtable();
		if(list!=null)
		for (int j = 0; j < list.size(); j++) {
			SysDict sysDict = (SysDict) list.get(j);
			hashtable.put(sysDict.getId(), sysDict.getDictName());

		}
		//SYS_DICT = hashtable;*/
	}

	public static String getDictValue(String key) {
		if (key != null && !key.equals("")) {
			String value = "";
		/*	if (SYS_DICT.get(key) != null) {
				value = SYS_DICT.get(key).toString();
			} else {
				ISysDictManager mgr = (ISysDictManager) ApplicationContextHolder.getApplicationContext()
						.getBean("ItawSystemDictManager");
				SysDict sysDict = (SysDict) mgr.getById(key);
				value = sysDict.getDictName();
				SYS_DICT.put(key, value);
			}*/
			return value;
		} else {
			return "";
		}
	}

	/*public static String getDeptValue(String key) {
		if (key != null && !key.equals("")) {
			String value = "";
			if (SYS_DEPT.get(key) != null) {
				value = SYS_DEPT.get(key).toString();
			} else {
				ISysDeptManager mgr = (ISysDeptManager) ApplicationContextHolder
						.getBean("ItawSystemDeptManager");
				SysDept tawSystemDept = mgr.getDeptinfobydeptid(key, "0");
				value = StaticMethod.null2String(tawSystemDept.getDeptName());
				SYS_DEPT.put(key, value);
			}
			return value;
		} else {
			return "";
		}
	}*/

	public static String getUserValue(String key) {
		if (key != null && !key.equals("")) {
			String value = "";
			if (SYS_USER.get(key) != null) {
				value = SYS_USER.get(key).toString();
			} else {
				ISysUserManager mgr = (ISysUserManager) ApplicationContextHolder.getApplicationContext()
				.getBean("sysUserManager");
				SysUser sysUser=mgr.getById(key);
				if(sysUser == null) return "";
				value = StaticMethod.null2String(sysUser.getUserName());
				SYS_USER.put(key, value);
			}
			return value;
		} else {
			return "";
		}
	}
	public static String getMsgStatus(String k){
		if(k != null){
	    	return (String)MSG_STATUS.get(k);
		}
		return "";
	}

}
